﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.FileFunction.Conversion
{
    /// <summary>
    /// 手动转换
    /// </summary>
    public static class XmlConvsersion
    {
        /// <summary>
        /// 手动转化为xml格式
        /// </summary>
        /// <typeparam name="TIn"></typeparam>
        /// <param name="T"></param>
        /// <returns></returns>
        public static string ToRootNodeXmlStr<TIn>(this TIn T) where TIn : class
        {
            var str = "";
            var type = typeof(TIn);
            foreach (var prop in type.GetProperties())
            {
                var value = type.GetProperty(prop.Name)?.GetValue(T);
                str += $"<{prop.Name}>{value}</{prop.Name}>";
            }

            foreach (var field in type.GetFields())
            {
                var value = type.GetProperty(field.Name)?.GetValue(T);
                str += $"<{field.Name}>{value}</{field.Name}>";
            }

            return $"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>{str}</root>";
        }

    }
}
